import {
  getFlowTable
} from '../../api/financial'
import {
  SERVICE_FEE_LIST,
  SERVICE_FEE_DATA_COUNT,
  SERVICE_FEE_HISTORY_DATA,
  SERVICE_FEE_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    serviceFeeList: [],
    serviceFeeDataCount: null,
    serviceFeePageSize: 20,
    serviceFeeHistoryData: [],
    row: {
      ctime: '',
      type0: '',
      type: '',
      money: '',
      des: ''
    }
  },
  getters: {
    serviceFeeList: state => state.serviceFeeList
  },
  mutations: {
    [SERVICE_FEE_LIST] (state, data) {
      state.serviceFeeList = data
    },
    [SERVICE_FEE_DATA_COUNT] (state, data) {
      state.serviceFeeDataCount = data
    },
    [SERVICE_FEE_HISTORY_DATA] (state, data) {
      state.serviceFeeHistoryData = data
    },
    [SERVICE_FEE_PAGE_SIZE] (state, data) {
      state.serviceFeePageSize = data
    }
  },
  actions: {
    async getServiceFeeList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      let sumServiceFee = ''
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getFlowTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        // if (item.type === 1) {
        newData = [...newData, {
          id: item.id,
          ctime: item.ctime,
          type: item.type,
          type0: item.type0,
          money: item.money,
          des: item.des,
          allServiceFee: 5000
        }]
        // }
      })
      commit(SERVICE_FEE_LIST, newData)
      commit(SERVICE_FEE_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.serviceFee.serviceFeePageSize) {
        commit(SERVICE_FEE_HISTORY_DATA, newData)
      } else {
        commit(SERVICE_FEE_HISTORY_DATA, newData.slice(0, rootState.serviceFee.serviceFeePageSize))
      }
      return resp
    },
    async changeServiceFeePage ({ commit, rootState }, index) {
      let data = rootState.serviceFee.serviceFeeList
      let _start = (index - 1) * rootState.serviceFee.serviceFeePageSize
      let _end = index * rootState.serviceFee.serviceFeePageSize
      commit(SERVICE_FEE_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeServiceFeePageSize ({ commit, rootState }, value) {
      commit(SERVICE_FEE_PAGE_SIZE, value)
    }
  }
}
